using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Globalization;
using System.Xml;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Colorprj.Masters
{

    enum AccType {Customer,Supplier,Cash,Expenses,Bank, Sale,Purchase }

    class BuyerManager
    {
        public DataSet GetAllBuyers()
        {
            // Create the Database object, using the default database service. The
            // Create the Database object, using the default database service. The
            // default database service is determined through configuration.
            Database db = DatabaseFactory.CreateDatabase();

            // Two operations, one to credit an account, and one to debit another
            // account.
            string sqlCommand = "SelAllBuyer";
            DbCommand selSPCommand = db.GetStoredProcCommand(sqlCommand);
            // DataSet that will hold the returned results		
            DataSet productsDataSet = null;

            productsDataSet = db.ExecuteDataSet(selSPCommand);

            // Note: connection was closed by ExecuteDataSet method call 

            return productsDataSet;
        }

        public DataSet GetAllBuyers(int acctype)
        {
            // Create the Database object, using the default database service. The
            // Create the Database object, using the default database service. The
            // default database service is determined through configuration.
            Database db = DatabaseFactory.CreateDatabase();

            // Two operations, one to credit an account, and one to debit another
            // account.
            string sqlCommand = "SelAllBuyer";
            DbCommand selSPCommand = db.GetStoredProcCommand(sqlCommand);
            db.AddInParameter(selSPCommand, "AccType", DbType.Int16, acctype);
            // DataSet that will hold the returned results		
            DataSet productsDataSet = null;

            productsDataSet = db.ExecuteDataSet(selSPCommand);

            // Note: connection was closed by ExecuteDataSet method call 

            return productsDataSet;
        }


        public DataSet GetAllAccType()
        {
            // Create the Database object, using the default database service. The
            // Create the Database object, using the default database service. The
            // default database service is determined through configuration.
            Database db = DatabaseFactory.CreateDatabase();

            // Two operations, one to credit an account, and one to debit another
            // account.
            string sqlCommand = "SelAllAccType";
            DbCommand selSPCommand = db.GetStoredProcCommand(sqlCommand);
            // DataSet that will hold the returned results		
            DataSet productsDataSet = null;

            productsDataSet = db.ExecuteDataSet(selSPCommand);

            // Note: connection was closed by ExecuteDataSet method call 

            return productsDataSet;
        }



        public bool AddBuyer(Buyer pEntity)
        {
            bool result = false;
            Database db = DatabaseFactory.CreateDatabase();
            using (DbConnection connection = db.CreateConnection())
            {
                connection.Open();
                DbTransaction transaction = connection.BeginTransaction();

                try
                {


                    // Two operations, one to credit an account, and one to debit another
                    // account.
                    string sqlCommand = "AddBuyer";
                    DbCommand addGroupCommand = db.GetStoredProcCommand(sqlCommand);
                    db.AddInParameter(addGroupCommand, "BuyerName", DbType.String, pEntity.BuyerName);
                    db.AddInParameter(addGroupCommand, "BuyerAddress1", DbType.String, pEntity.BuyerAddress1);
                    db.AddInParameter(addGroupCommand, "BuyerAddress2", DbType.String, pEntity.BuyerAddress2);
                    db.AddInParameter(addGroupCommand, "BuyerCity", DbType.String, pEntity.BuyerCity);
                    db.AddInParameter(addGroupCommand, "PhoneNo", DbType.String, pEntity.PhoneNo);
                    db.AddInParameter(addGroupCommand, "EmailId", DbType.String, pEntity.EmailId);

                    db.AddInParameter(addGroupCommand, "AccType", DbType.Int16, pEntity.AccType);
                    db.AddInParameter(addGroupCommand, "OpBal", DbType.Double, pEntity.OpBal);
                    db.AddInParameter(addGroupCommand, "DrCr", DbType.String, pEntity.DrCr);
                    db.AddInParameter(addGroupCommand, "TinNo", DbType.String, pEntity.TinNo);

                    // Credit the first account
                    db.ExecuteNonQuery(addGroupCommand, transaction);

                    transaction.Commit();
                    result = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
            }
            return result;
        }

        public bool UpdBuyer(Buyer pEntity)
        {
            bool result = false;
            Database db = DatabaseFactory.CreateDatabase();
            using (DbConnection connection = db.CreateConnection())
            {
                connection.Open();
                DbTransaction transaction = connection.BeginTransaction();

                try
                {


                    // Two operations, one to credit an account, and one to debit another
                    // account.
                    string sqlCommand = "UpdBuyer";
                    DbCommand addGroupCommand = db.GetStoredProcCommand(sqlCommand);
                    db.AddInParameter(addGroupCommand, "BuyerId", DbType.String, pEntity.BuyerId);
                    db.AddInParameter(addGroupCommand, "BuyerName", DbType.String, pEntity.BuyerName);
                    db.AddInParameter(addGroupCommand, "BuyerAddress1", DbType.String, pEntity.BuyerAddress1);
                    db.AddInParameter(addGroupCommand, "BuyerAddress2", DbType.String, pEntity.BuyerAddress2);
                    db.AddInParameter(addGroupCommand, "BuyerCity", DbType.String, pEntity.BuyerCity);
                    db.AddInParameter(addGroupCommand, "PhoneNo", DbType.String, pEntity.PhoneNo);
                    db.AddInParameter(addGroupCommand, "EmailId", DbType.String, pEntity.EmailId);
                    db.AddInParameter(addGroupCommand, "AccType", DbType.Int16, pEntity.AccType);
                    db.AddInParameter(addGroupCommand, "OpBal", DbType.Double, pEntity.OpBal);
                    db.AddInParameter(addGroupCommand, "DrCr", DbType.String, pEntity.DrCr);
                    db.AddInParameter(addGroupCommand, "TinNo", DbType.String, pEntity.TinNo);

                    // Credit the first account
                    db.ExecuteNonQuery(addGroupCommand, transaction);

                    transaction.Commit();
                    result = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
            }
            return result;
        }


        public void DelBuyer(int Id)
        {

            // Create the Database object, using the default database service. The
            // default database service is determined through configuration.
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                string sqlCommand = "DelBuyer";
                DbCommand selSPCommand = db.GetStoredProcCommand(sqlCommand);
                db.AddInParameter(selSPCommand, "Id", DbType.Int32, Id);
                db.ExecuteNonQuery(selSPCommand);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
    }
}
